#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
User: lyj
DateTime: 2012-05-20 14:22:00

"""

__author__ = 'lyj'



from demos.rabbitmq.settings import amqp_host, amqp_port
import pika
import sys
import time

from pika.adapters import BlockingConnection

pika.log.setup(color=True)

if __name__ == '__main__':
    # Connect to RabbitMQ
    #host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1'
    parameters = pika.ConnectionParameters(host=amqp_host, port=amqp_port)
    connection = BlockingConnection(parameters)

    # Open the channel
    channel = connection.channel()

    # Declare the queue
    channel.queue_declare(queue="test", durable=True,
        exclusive=False, auto_delete=False)

    # Initialize our timers and loop until external influence stops us
    count = 0
    start_time = time.time()
    while True:
        # Construct a message and send it
        message = "BlockingConnection.channel.basic_publish #%i" % count
        channel.basic_publish(exchange='',
            routing_key="test",
            body=message,
            properties=pika.BasicProperties(
                content_type="text/plain",
                delivery_mode=1))

        print " [x] Sent 'Message'"

        count += 1
        if count % 1000 == 0:
            duration = time.time() - start_time
            pika.log.info("%i Messages Sent: %.8f per second",
                count, count / duration)

        if count >= 2: break